(define (count-pairs x)
    (define store-pairs `())
    (define (push-pair! pair)
        ; (display "sp=")
        ; (display store-pairs)
        ; (display " pair=")
        ; (display pair)
        ; (newline)
        (set! store-pairs (cons pair store-pairs))
        ; (display "sp2=")
        ; (display store-pairs)
        ; (newline)
    )
    (define (has-pair? pair pair-list)
        (cond 
            ((null? pair-list) #f)
            ((eq-pair? pair (car pair-list)) #t)
            (else  (has-pair? pair (cdr pair-list)))             
        )    
    )
    (define (eq-pair? a b)
        (cond
            ((and (null? a) (null? b)) #t)
            ((and (pair? a) (pair? b)) 
                (and 
                    (eq-pair? (car a) (car b))
                    (eq-pair? (cdr a) (cdr b))
                )
            )
            ((and (not (pair? a)) (not (pair? b)))
                (eq? a b)
            )
            (else #f)    
        )
    )
    (define (iter x) 
        (cond 
            ((not (pair? x)) 0)
            ((has-pair? x store-pairs) 0)
            (else 
                (begin
                    (push-pair! x)
                    (+ 
                        (iter (car x))
                        (iter (cdr x))
                        1
                    )
                )       
            )
        )
    )
    (iter x)
)

;3个
(define A0 (cons (cons 1 2) (cons 3 4 )))
;4个
(define A1 (cons 1 2))
(define B1 (cons A1 A1))
(define C1 (cons B1 3))
;7个
(define A2 (cons 1 2))
(define B2 (cons A2 A2))
(define C2 (cons B2 B2))

(display (count-pairs A0))
(newline)
(display (count-pairs C1))
(newline)
(display (count-pairs C2))

(exit)